/*!
 * @文件名 drv_master_iic.c
 *
 * @功能描述
 *  1.S32K118的flex_io作为模拟IIC总线，MCU为主机，三个芯片为从机
 *  2.通常由其他三个芯片的bsp文件调用，以及由上层直接调用
 */
#include "drv_master_iic.h"

static uint32_t s_timeout = I2C_TIMEOUT;
static status_t status = STATUS_SUCCESS;
static uint32_t s_byteRemaining = 8U;
static bool ACK;

/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_master_iic_init
 * Description   : IIC模块主机初始化配置函数
 *
 *END**************************************************************************/
void bsp_drv_master_iic_init(void)
{
	I2C_MasterInit(&flexio_instance, &i2c_pal_flexio_MasterConfig0);
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_master_iic_deinit
 * Description   : IIC模块去初始化函数
 *
 *END**************************************************************************/
void bsp_drv_master_iic_deinit(void)
{
	I2C_MasterDeinit(&flexio_instance);
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_maste_iic_set_slave_address
 * Description   : 修改目标从机地址，用以选择不同的芯片进行通信
 *
 *END**************************************************************************/
int bsp_drv_maste_iic_set_slave_address(uint16_t address)
{
	return I2C_MasterSetSlaveAddress(&flexio_instance, address, false);
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_master_iic_read
 * Description   : IIC读函数，将读出来的数据存在buffer中，并将是否成功读取返回
 *
 *END**************************************************************************/
bool bsp_drv_master_iic_read(uint8_t *pBuf,int len ,bool sendstop)
{
	 s_timeout = I2C_TIMEOUT;
	 status = I2C_MasterReceiveData(&flexio_instance, pBuf, len, sendstop);
	 if (status == STATUS_SUCCESS)
	 {
	     do
	     {
	         s_timeout--;
	         status = I2C_MasterGetTransferStatus(&flexio_instance, &s_byteRemaining);
	     } while ((s_timeout != 0U) && (status != STATUS_SUCCESS));
	     if ((s_byteRemaining != 0U) || (s_timeout == 0U))
	     {
	         return false;
	     }
	     else
	     {
	         return true;
	     }
	 }
	 else
	 {
	     return false;
	 }
}/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_master_iic_write
 * Description   : IIC写函数，将buffer中的数据发送给目标从机，并将是否成功写入返回
 *
 *END**************************************************************************/
bool bsp_drv_master_iic_write(const uint8_t *pBuf,int len, bool sendstop)
{
    s_timeout = I2C_TIMEOUT;
    status = I2C_MasterSendData(&flexio_instance, pBuf, len, sendstop);
    if (status == STATUS_SUCCESS)
    {
        do
        {
            s_timeout--;
            status = I2C_MasterGetTransferStatus(&flexio_instance, &s_byteRemaining);
        } while ((s_timeout != 0U) && (status != STATUS_SUCCESS));
        if ((s_byteRemaining != 0U) || (s_timeout == 0U))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_ctrl_write_register
 * Description   : 先修改目标从机地址,然后写buffer到芯片，buffer包含寄存器地址和具体数据
 *
 *END**************************************************************************/
bool bsp_ctrl_write_register(uint16_t address, uint8_t *buf,  uint8_t len)
{
	bsp_drv_maste_iic_set_slave_address(address);
    ACK =  bsp_drv_master_iic_write(buf,len,false);
    return ACK;
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_ctrl_read_register
 * Description   : 先修改目标从机地址，然后写一位寄存器地址到器件，后续将该寄存器数据读到buffer中
 *
 *END**************************************************************************/
bool bsp_ctrl_read_register(uint16_t address,uint8_t pos, uint8_t *buf,  uint8_t len)
{
	bsp_drv_maste_iic_set_slave_address(address);
    ACK =  bsp_drv_master_iic_write(&pos,1,false);
    if(ACK == true)
        ACK =  bsp_drv_master_iic_read(buf,len,true);
     return ACK;
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_ctrl_read_pair_register
 * Description   : 配置输入端口
 *
 *END**************************************************************************/
bool bsp_ctrl_read_pair_register(uint16_t address,short pos, uint8_t *buf,  uint8_t len)
{
	uint8_t array[2] = {(pos>>8)&0xFF,(pos)&0xFF};
    ACK =  bsp_drv_master_iic_write(array,2,false);
    if(ACK)
    {
    	bsp_drv_maste_iic_set_slave_address(address);
        ACK =  bsp_drv_master_iic_read(buf,len,true);
    }
     return ACK;
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_ctrl_wirte_polling
 * Description   : 配置输入端口
 *
 *END**************************************************************************/
void bsp_ctrl_wirte_polling(uint16_t address,uint8_t* s_transmitBuffer,uint32_t I2C_TX_BUFFER_LENGTH)
{
	(void)I2C_TX_BUFFER_LENGTH;
	bsp_drv_maste_iic_set_slave_address(address);
	bsp_drv_master_iic_write(s_transmitBuffer,I2C_BUFFER_LENGTH,true);
}
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_ctrl_read_polling
 * Description   : 配置输入端口
 *
 *END**************************************************************************/
void bsp_ctrl_read_polling(uint16_t address, uint8_t* s_receiveBuffer,uint32_t I2C_RX_BUFFER_LENGTH)
{
	(void)I2C_RX_BUFFER_LENGTH;
	bsp_drv_maste_iic_set_slave_address(address);
	bsp_drv_master_iic_read(s_receiveBuffer,I2C_BUFFER_LENGTH,true);
}
